To use the particle routines in your own project, add SWParticles.c to your project, and #include SWParticles.h.
With just a few calls you can add some pretty spiffy effects to your SpriteWorld. To set things up, call InitParticles, and install the appropriate postEraseCallBack and postDrawCallBack (depending on whether your animation is scrolling or non-scrolling). Then simply call NewParticle whenever you want to add a new particle to the animation. To run the animation, call UpdateParticlesInWindow or UpdateParticlesInScrollingWindow once each frame in your main animation loop.
Fix-point numbers
To get the fix-point values easily, use the type SWFixed and these macros:
SWFixed SW_FLOAT2FIX(float x);
float SW_FIX2FLOAT(SWFixed x);
SWFixed SW_INT2FIX(int x);
int SW_FIX2INT(SWFixed x);
But be aware that the float <-> fixed conversions will be slow, even on a PPC, as any conversion between floating point and integer will be slow.
You may safely add and subtract fixed-point numbers with each other. You may also use multiplication and division, provided that one of the numbers is fixed-point, and the other is a normal integer. For example, the following are all correct:
fixedNum = fixedNum / intNum;
fixedNum = intNum / fixedNum;
fixedNum = intNum * fixedNum;
The only danger here is that you might have overflow when doing multiplication, although this is unlikely, considering that the range of a SWFixed number is 8388607.255 through -8388608.255.
You don't have to include SWFixed.c unless you want the trig tables too.
Then you must set them up with a call SWInitTrigTables.
Call this once when you start up. Allocates space for the particle list.
NumParticles is the max number of particles to track. Gravity is an amount applied to each particle every frame. You'll most likely want to use 1 or less. An error code will be returned if the memory allocation for the particle array fails.
ClearParticles
SW_FUNC void ClearParticles( void );
Call this if you want to clear out the current particles. It doesn't erase
them from the screen though - you need to use
SWCopyBackgroundToWorkArea(spriteWorldP) and
SWUpdateSpriteWorld(spriteWorldP, true) to update the window.
NewParticle
SW_FUNC void NewParticle(
unsigned long color,
SWFixed horizLoc,
SWFixed vertLoc,
SWFixed horizSpeed,
SWFixed vertSpeed,
short lifeRemaining);
Adds a new particle to the animation.
color is a color index. Use Color2Index to convert from a rgb color to the index.
lifeRemaining tells the particle library how many frames the particle exists before being deleted. The particle will not be added if the particleArray is too full. (InitParticles defines the maximum number of particles you can have at any time.)
Call once in your animation loop to update the particles
You will want to set each of your Sprite's needsToBeDrawn flags to true when using particles, as the particles will leave "holes" in your Sprites if they move over idle Sprites.
-------------
These routines were originally written by Matthew Foodim (MatthewF@Panix.com), and updated by Anders F Björklund (afb@algonet.se) to do 16-bit and 32-bit, as well as fixed-point. Vern Jensen (Jensen@loop.com) cleaned up and optimized the particle engine and demos.